#include				<iostream>
#include				"Square.hh"

Square::Square(int posX, int posY)
	: _posX(posX), _posY(posY), _smell(0)
{
}

Square::~Square()
{

}

void					Square::init(std::string stonePath)
{
	if (!_display)
	{
		_stonePath = stonePath;
		_stoneImage.LoadFromFile(stonePath);
		_stone.SetImage(_stoneImage);
		_stone.SetPosition(_posX, _posY);
		setSmell(0, OWN_COORD, (((stonePath) == (BLACK_STONE_PATH)) ? false : true));
		_display = true;
	}
}

bool			const	Square::displayable() const
{
	return (_display);
}

int 			const	Square::getSmell() const
{
	return (_smell);
}

char 					Square::getBytePos(int smellPower, short direction)
{
	if (direction < OWN_COORD)
		return (4 - smellPower);
	else if (direction > OWN_COORD)
		return (4 + smellPower);
	return (4);
}

void					Square::setSmell(int smellPower, short direction, bool color)
{
	int				decalValue = (int)getBytePos(smellPower, direction);

	//Set position du pion poser dans le dernier octet (Octet dedier au pattern de pierres)
	_smell |= (1 << (decalValue));
	//Set couleur du pion poser dans l'avant dernier octet (Octet dedier au pattern couleur)
	if (color)
		_smell |= (1 << (decalValue + 9));
	//Set direction d'où viens l'odeur (Chaque bit correspond à une direction)
	_smell |= (1 << (18 + direction));
/*	if (BOX_NUM(_posX, _posY) >= 176 && BOX_NUM(_posX,_posY) <= 180)
	{
		std::cout << "KEY => " << BOX_NUM(_posX, _posY) << " SmellPower " << smellPower << std::endl;
	unsigned int bit = 0;
	for (int i = 31 ; i >= 0 ; --i)
	{
		bit = (_smell >> i) & 1;
		if (i < 27)
			std::cout << bit;
		if (i % 9 == 0 && i < 27)
			std::cout << " ";
	}
	std::cout << std::endl;
	}*/
}

sf::Sprite		const	&Square::getStone() const
{
	return (_stone);
}
